﻿using DataScripter.Database;
using DataScripter.Scripting;
using DataScripter.Scripting.Filters;
using DataScripter.Scripting.Fluent;
using DataScripter.Scripting.InputSources;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;

namespace DataScripter.UnitTests.FluentScriptRequestTests
{
    [TestClass]
    public class WhenFilteringByForeignKey
    {
        private const string MY_FOREIGN_KEY = "MyForeignKey";
        private readonly IFluentTableScriptRequest _fluentTableRequest;
        private readonly IFluentTableScriptRequest _result;

        public WhenFilteringByForeignKey()
        {
            Engine engine = new Engine();
            IInputSource inputSource = new SqlServerInputSource("", engine);
            engine.RegisterType(inputSource);
            Mock<IDatabaseStructure> databaseStructureMock = new Mock<IDatabaseStructure>();
            databaseStructureMock.Setup(ds => ds.GetTable("MyTable")).Returns((ITableStructure) null);
            ITableScriptRequest tableScriptRequest = new TableScriptRequest("MyTable", databaseStructureMock.Object, 0);
            _fluentTableRequest = new FluentTableScriptRequest(null, tableScriptRequest, engine.Resolve<IInputSource>());
            Mock<IForeignKey> myForeignKeyMock = new Mock<IForeignKey>();
            myForeignKeyMock.Setup(fk => fk.Name).Returns(MY_FOREIGN_KEY);
            _result = _fluentTableRequest.FilterByForeignKey(myForeignKeyMock.Object);
        }

        [TestMethod]
        public void ResultAndOriginalFluentRequestAreTheSame()
        {
            Assert.AreSame(_fluentTableRequest, _result);
        }

        [TestMethod]
        public void FilterIsRecorded()
        {
            Assert.IsTrue(_fluentTableRequest.Request.Filters.ContainsKey(MY_FOREIGN_KEY));
        }

        [TestMethod]
        public void RequestGetsForeignKeyFilter()
        {
            Assert.IsTrue(_fluentTableRequest.Request.Filters[MY_FOREIGN_KEY] is IForeignKeyFilter);
        }
    }
}
